#ifndef RENDERTHREAD_H
#define RENDERTHREAD_H

#include <QWidget>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>
#include <QtGui>

#include "Field.h"
#include "Object.h"
#include "Object.h"

  // thread for rendering QImage ( game picture )
class RenderThread : public QThread
{
    Q_OBJECT

    public:
          // must be >= IMAGEWIDTH ( might work in other case, but it's
          //                         not guaranteed )
        static const int WIDTH  = 900;
          // must be >= IMAGEHEIGHT ( might work in other case, but it's
          //                          not guaranteed )
        static const int HEIGHT = 900;

        static const int IMAGEWIDTH  = 640;
        static const int IMAGEHEIGHT = 480;

        RenderThread()
            :render( QImage( WIDTH,  // WIDTH  *  Field::WIDTH,
                             HEIGHT, //HEIGHT *  Field::HEIGHT,
                             QImage::Format_RGB32 ) ),
              painter( new QPainter( &render ) ),
              zoomToPlayer_( true ), zoomX( 20 ), zoomY( 20 ),
              zoomCenterX( Field::WIDTH / 2 ),
              zoomCenterY( Field::HEIGHT / 2 )
        {}

        ~RenderThread() {
            this->exit();
            delete painter;
            this->terminate();
        }

        void paint( int, int, int, int, bool, Field *f );

        void setZoomToPlayer( bool i ) { zoomToPlayer_ = i; }
        void setZoomX( int i ){ zoomX = i; }
        void setZoomY( int i ){ zoomY = i; }
        void setZoomCenterX( int i ) { zoomCenterX = i; }
        void setZoomCenterY( int i ) { zoomCenterY = i; }

        void setImage( QImage* i ){ render = *i; }
        void setField( Field* i ) { f = i; }

        bool zoomToPlayer() const { return zoomToPlayer_; }
        int getZoomX() const { return zoomX; }
        int getZoomY() const { return zoomY; }
        int getZoomCenterX() const { return zoomCenterX; }
        int getZoomCenterY() const { return zoomCenterY; }
        QImage* getImage() { return &render; }
        Field* getField() { return f; }
        QPainter* getPainter(){ return painter; }
    signals:
    public slots:
        void needsPainting( int x, int y, int x2,
                            int y2, bool zoom, Field* f ) { paint( x, y, x2,
                                                                   y2, zoom, f ); }
        void needsDrawing( GameWidget* widget, bool halluMode );
    protected:
        void run();
    private:
          // switches object->getSymbol(),
          // decides what to draw
        QString whatToDraw( Object* o );

        QMutex mutex;
        QWaitCondition condition;
        QImage render;
        QPainter* painter;

        Field* f;
          // is true in case of zooming to player,
          // postfix for fixing name conflict
        bool zoomToPlayer_;
          // zoom factor
        int zoomX, zoomY;
          // zoom center
        int zoomCenterX, zoomCenterY;
};

#endif // RENDERTHREAD_H
